package com.giornoadd.timex.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.giornoadd.timex.dao.TimesheetManager;
import com.giornoadd.timex.model.Employee;
import com.giornoadd.timex.model.Timesheet;
import com.giornoadd.timex.util.ApplicationSecurityManager;
import com.giornoadd.timex.util.TimexJmxBean;

/**
 * Controller for the Timesheet List screen.
 * @author anil 
 */
@Controller
@RequestMapping("/timesheet")
public class TimesheetListController 
{
	@Autowired
    private TimesheetManager timesheetManager;
	@Autowired
    private TimexJmxBean timexJmxBean;
	@Autowired
    private ApplicationSecurityManager applicationSecurityManager;

    /**
     * Returns a list of Timesheet database objects in ModelAndView.
     * @see com.visualpatterns.timex.model.Timesheet
     */
    @RequestMapping(method=RequestMethod.GET)
    public ModelAndView handleRequest(HttpSession session)
    {
        Employee employee = (Employee) applicationSecurityManager.getEmployee(session);
    	Map<String, Object> model = new HashMap<String, Object>();
	        List<Timesheet> timesheets = timesheetManager.getTimesheets(employee.getEmployeeId());
	        timexJmxBean.setTimesheetsFetched(timexJmxBean.getTimesheetsFetched()
	                + timesheets.size());
	        model.put("timesheets", timesheets);
	        return new ModelAndView("timesheetlist", model);
    }

    public TimesheetManager getTimesheetManager()
    {
        return timesheetManager;
    }

    public void setTimesheetManager(TimesheetManager timesheetManager)
    {
        this.timesheetManager = timesheetManager;
    }

    public ApplicationSecurityManager getApplicationSecurityManager()
    {
        return applicationSecurityManager;
    }

    public void setApplicationSecurityManager(
            ApplicationSecurityManager applicationSecurityManager)
    {
        this.applicationSecurityManager = applicationSecurityManager;
    }

    public TimexJmxBean getTimexJmxBean()
    {
        return timexJmxBean;
    }

    public void setTimexJmxBean(TimexJmxBean timexJmxBean)
    {
        this.timexJmxBean = timexJmxBean;
    }
}
